<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>自定义Promise</title>
  </head>
  <body>
    <script src="./03-Promise.js"></script>
    <!-- <script>
      const p = new Promise((resolve, reject) => {
        setTimeout(() => {
          // resolve(1);
          reject(2);
          console.log("reject()状态改变之后");
        }, 100);
      });

      p.then(
        (value) => {
          console.log("onResolved()", value);
        },
        (reason) => {
          console.log("onRejected()", reason);
        }
      );

      p.then(
        (value) => {
          console.log("onResolved2()", value);
        },
        (reason) => {
          console.log("onRejected2()", reason);
        }
      );
    </script> -->

    <!-- 测试then方法 -->
    <!-- <script>
      const p = new Promise((resolve, reject) => {
        setTimeout(() => {
          // resolve(1)
          reject(2);
        }, 100);
      })
        .then(
          (value) => {
            console.log("onResolved()", value);
          },
          (reason) => {
            console.log("onRejected()", reason);
            // return 3
            // throw 4
            return new Promise((resolve, reject) => reject(5));
          }
        )
        .then(
          (value) => {
            console.log("onResolved2()", value);
          },
          (reason) => {
            console.log("onRejected2()", reason);
            throw 6;
          }
        )
        .catch((reason) => {
          console.log("onRejected3()", reason);
          // 中断promise链，阻止它往后调
          return new Promise(()=>{})
        })
        .then(
          (value) => {
            console.log("onResolved4()", value);
          },
          (reason) => {
            console.log("onRejected4()", reason);
          }
        );
    </script> -->

    <!-- 测试 resolve / reject 方法 -->
    <!-- <script>
    const p1 = Promise.resolve(2)
    const p2 = Promise.resolve(Promise.resolve(3))
    const p3 = Promise.resolve(Promise.reject(4))

    p1.then(value=>{console.log('p1',value)})
    p2.then(value=>{console.log('p2',value)})
    p3.catch(reason=>{console.log('p3',reason)})

  </script> -->

    <!-- 测试all方法 -->
    <script>
      const p1 = Promise.resolve(2);
      const p2 = Promise.resolve(Promise.resolve(3));
      const p3 = Promise.resolve(Promise.reject(4));
      const p4 = new Promise((resolve) => {
        setTimeout(() => {
          resolve(5);
        }, 1000);
      });
      const p5 = Promise.reject(6);

      const pAll = Promise.all([p1, p2,9]);
      const pRace = Promise.race([p4, p5, p1, p2, p3,9]);

      pAll.then(
        (values) => {
          console.log("all onResolved()", values);
        },
        (reason) => {
          console.log("all onRejected()", reason);
        }
      );

      pRace.then(
        (value) => {
          console.log("race onResolved()", value);
        },
        (reason) => {
          console.log("race onRejected()", reason);
        }
      );
    </script>
  </body>
</html>
